import type IBase from '../define/base'
import type { AsyncDataResult, SyncDataResult } from '../define/types'
import { primaryKey, v4Id, sqliteTypes } from '../utils'
import knex from '../connection'

class Base implements IBase {
  uuid(): SyncDataResult<string> {
    return v4Id()
  }

  types(): SyncDataResult<string> {
    return sqliteTypes
  }

  showPrimaryKey(): SyncDataResult<string> {
    return primaryKey()
  }

  async showTables(): AsyncDataResult<string> {
    const exclude = ['sqlite_sequence']
    const rs = await knex('sqlite_master')
      .where({ type: 'table' })
      .select('name')
    return rs.map((r) => r.name).filter((k) => {
      return !exclude.includes(k)
    })
  }
}

export default new Base()